package com.situ.mall.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.common.entity.Category;
import com.situ.mall.common.service.ICategoryService;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JavaWizard
 * @since 2023-09-12
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //打开分类管理页面
    @GetMapping("/page")
    public String page(){
        return "category";
    }

    //查询树形表格
    @GetMapping("/tree")
    @ResponseBody
    public TableVO tree(Integer page, Integer limit, String name,Integer parentId){
        return categoryService.tree(page,limit,name,parentId);
    }

    //保存
    @PostMapping("/save")
    @ResponseBody
    public void save(Category category){
        categoryService.saveCategory(category);
    }

    //删除
    @PostMapping("/delete/{categoryId}")
    @ResponseBody
    public void delete(@PathVariable("categoryId")Integer id){
        categoryService.delete(id);
    }

    //编辑——根据ID查询该行数据带到前台
    @GetMapping("/selectById/{id}")
    @ResponseBody
    public Category selectById(@PathVariable Integer id){
        return categoryService.selectById(id);
    }

    //发请求去查所有的分类数据
    @GetMapping("/all")
    @ResponseBody
    public List<Category> all(){
        return categoryService.all();
    }


    //根据parent_id = id 查分类
    //传id=0时，查一级分类；传id=？时，查下级分类
    @GetMapping("/children")
    @ResponseBody
    public List<Category> second(Integer id){
        //查询的条件构造器
        QueryWrapper<Category> wq = new QueryWrapper<>();
        //条件为：表中数据“parent_id” == id
        wq.eq("parent_id",id);
        return categoryService.list(wq);
    }
}
